#!/usr/bin/env python2.5
# encoding: utf-8

if not globals().has_key("esda"):
    execfile("esda.py")
    sys.exit()

init()
from StreamLoader import *

# logFile = "shortMarkerClockData.csv"
logFile = "markerClockData.csv"

print """
----
Starting IMPORT of %s
----
"""%logFile

CSVMillisecondTimeStreamLoader(newStream, logFile).run()

newFilterStream(
    "CorrectedJulieMotion", "julie_MOTION",
    valueFilter=lambda startTime, endTime, value: value if startTime > 1181309400 else value * 500)

newFilterStream(
    "CorrectedLionelMotion", "lionel_MOTION",
    valueFilter=lambda startTime, endTime, value: value if startTime > 1181309400 else value * 500)

def aggregationStartTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    aggregationStartTime = int(startTime / 86400) * 86400
    context['result'] = aggregationStartTime

def aggregationEndTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    context['result'] = max(aggregationStartTime + 86400, int(endTime / 86400) * 86400)

def aggregationValueIterator(context, startTime, endTime, value):
    context['result'] = value + context['result'] if context['result'] != None else value

def aggregationGrouper(startTime, endTime, value):
    return int(startTime / 86400)

newAggregationStream(
    "DailyAggregationOfJulie", "CorrectedJulieMotion",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)

newAggregationStream(
    "DailyAggregationOfLionel", "CorrectedLionelMotion",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)

newFilterStream(
    "ActiveJulie", "CorrectedJulieMotion",
    filter=lambda startTime, endTime, value: True if value > 25000 else False)

newOperatorStream(
    "JulieMarkersWhenActive", "ActiveJulie", "julie_NUM_MARKER",
    lambda activeValue, numMarkers: numMarkers)

hide((0, END))
show(2)
show(8)
show(9)
show(10)
show(11)
show(13)
